<template>
  <div class="app-container">
    <el-form :inline="true" :model="queryParams" label-width="68px" ref="queryForm">
      <el-form-item label="活动名称" prop="title">
        <el-input
          @keyup.enter.native="handleQuery"
          clearable
          placeholder="请输入活动名称"
          size="small"
          v-model="queryParams.title"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery" icon="el-icon-search" size="mini" type="primary">搜索</el-button>
        <el-button @click="resetQuery" icon="el-icon-refresh" size="mini">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          @click="handleAdd"
          icon="el-icon-plus"
          size="mini"
          type="primary"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          :disabled="single"
          @click="handleUpdate"
          icon="el-icon-edit"
          size="mini"
          type="success"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          :disabled="multiple"
          @click="handleDelete"
          icon="el-icon-delete"
          size="mini"
          type="danger"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          @click="handleExport"
          icon="el-icon-download"
          size="mini"
          type="warning"
        >导出
        </el-button>
      </el-col>
    </el-row>

    <el-table :data="beautyList" @selection-change="handleSelectionChange" v-loading="loading">
      <el-table-column align="center" type="selection" width="55"/>
      <el-table-column align="center" label="标题" prop="title"/>
      <el-table-column align="center" label="浏览量" prop="viewNum"/>
      <el-table-column align="center" label="点赞量" prop="clickNum"/>
      <el-table-column align="center" label="添加时间" prop="createTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column :formatter="shelfStatusFormat" align="center" label="上架状态" prop="shelfStatus"/>
      <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
        <template slot-scope="scope">
          <el-button
            @click="handleUpdate(scope.row)"
            icon="el-icon-edit"
            size="mini"
            type="text"
          >修改
          </el-button>
          <el-button
            @click="handleDelete(scope.row)"
            icon="el-icon-delete"
            size="mini"
            type="text"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      :limit.sync="queryParams.pageSize"
      :page.sync="queryParams.pageNum"
      :total="total"
      @pagination="getList"
      v-show="total>0"
    />

    <!-- 添加或修改风采展示对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body width="800px" height="800px">
      <el-form :model="form" :rules="rules" label-width="80px" ref="form">
        <el-form-item label="支部名称" prop="party">
          <el-input placeholder="请输入支部名称" v-model="form.party"/>
        </el-form-item>
        <el-form-item label="活动名称" prop="title">
          <el-input placeholder="请输入活动名称" v-model="form.title"/>
        </el-form-item>
        <el-row>
          <el-form-item label="缩略图">
            <el-upload
              :limit="limit"
              :multiple="multipleImg"
              :file-list="fileList"
              :action="action"
              list-type="picture-card"
              :on-success="uploadSuccess"
              :on-remove="handleRemove"
              :on-exceed="exceed"
              class="avatar-uploader"
            >
              <i class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
            <el-dialog :visible.sync="dialogVisible">
              <img width="100%" :src="dialogImageUrl" alt="">
            </el-dialog>
          </el-form-item>
          <el-col :span="24">
            <el-form-item label="上架状态">
              <el-radio-group v-model="form.shelfStatus">
                <el-radio
                  :key="dict.dictValue"
                  :label="dict.dictValue"
                  v-for="dict in shelfStatusOptions"
                >{{dict.dictLabel}}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="24">
          </el-col>
        </el-row>
        <el-form-item label="简介" prop="describes">
          <Editor v-model="form.describes"/>
        </el-form-item>

      </el-form>
      <div class="dialog-footer" slot="footer" style="padding-top:20px">
        <el-button @click="submitForm" type="primary">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {listBeauty, getBeauty, delBeauty, addBeauty, updateBeauty, exportBeauty} from "@/api/system/beauty";
  import Editor from '@/components/Editor'

  export default {
    name: "Beauty",
    components: {
      Editor
    },
    data() {
      return {
        // 图片上传
        limit: 6,
        multipleImg: true,
        dialogImageUrl: '',
        dialogVisible: false,
        disabled: false,
        imgList: [],
        fileList: [],

        action: process.env.VUE_APP_BASE_API + '/system/upload/file',
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 总条数
        total: 0,
        // 风采展示表格数据
        beautyList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 数据字典
        shelfStatusOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          party: undefined,
          title: undefined,
          describes: undefined,
          cover: undefined,
          shelfStatus: undefined,
          viewNum: undefined,
          clickNum: undefined,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {}
      };
    },
    created() {
      this.getList();
      this.getDicts("sys_shelf_status").then(response => {
        this.shelfStatusOptions = response.data;
      });
    },
    methods: {
      // 图片上传
      exceed() {
        this.msgInfo(`最多上传${this.limit}张图片`);
      },
      handleRemove(file, fileList) {
        this.fileList = fileList.map(res => {
          if (res.raw) {
            return {
              name: res.name,
              id: res.uid,
              status: res.status,
              raw: res.raw,
              url: res.url
            }
          } else {
            return {
              name: res.name,
              id: res.id,
              status: res.status,
              url: res.url
            }
          }
        })
        console.log('handleRemove', file, fileList, this.fileList);
        this.imgList = []
        this.fileList.map(res => {
          this.imgList.push(res.url)
        })
      },
      handlePictureCardPreview(file) {
        this.dialogImageUrl = file.url;
        this.dialogVisible = true;
      },
      uploadSuccess(e, file, fileList) {
        console.log('uploadSuccess', e, file, fileList)
        this.fileList = fileList.map(res => {
          if (res.raw) {
            return {
              name: res.name,
              id: res.uid,
              status: res.status,
              raw: res.raw,
              url: res.url,
              response: res.response.code ? res.response.data.path : res.response
            }
          } else {

            return {
              name: res.name,
              id: res.id,
              status: res.status,
              url: res.url
            }
          }
        })
        this.imgList = []
        this.fileList.map(res => {
          this.imgList.push(res.response)
        })
        console.log('fileList', this.fileList, this.imgList)
      },

      /** 查询风采展示列表 */
      getList() {
        this.loading = true;
        listBeauty(this.queryParams).then(response => {
          this.beautyList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 状态字典翻译
      shelfStatusFormat(row, column) {
        return this.selectDictLabel(this.shelfStatusOptions, row.shelfStatus);
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          beautyId: undefined,
          party: undefined,
          title: undefined,
          describes: undefined,
          cover: undefined,
          shelfStatus: 0,
          viewNum: undefined,
          clickNum: undefined,
          createBy: undefined,
          createTime: undefined,
          updateBy: undefined,
          updateTime: undefined,
          remark: undefined
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.beautyId)
        this.single = selection.length != 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.fileList = []
        this.title = "添加风采展示";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const beautyId = row.beautyId || this.ids
        getBeauty(beautyId).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改风采展示";
          if (this.form.cover) {
            this.imgList = this.form.cover.split(",")
            console.log(this.form.cover, 'imgList', this.imgList)
            this.fileList = this.imgList.map(res => {
              return {
                url: res
              }
            })
          }
          console.log(this.fileList)

        });
      },
      /** 提交按钮 */
      submitForm: function () {
        this.$refs["form"].validate(valid => {
          this.form.cover = this.imgList.join(',')
          this.fileList = []
          console.log('submit cover', this.form.cover, this.imgList)
          if (valid) {
            if (this.form.beautyId != undefined) {
              updateBeauty(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("修改成功");
                  this.open = false;
                  this.getList();
                }
              });
            } else {
              addBeauty(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("新增成功");
                  this.open = false;
                  this.getList();
                }
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const beautyIds = row.beautyId || this.ids;
        this.$confirm('是否确认删除风采展示编号为"' + beautyIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return delBeauty(beautyIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function () {
        });
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有风采展示数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return exportBeauty(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function () {
        });
      },


    }
  };
</script>

<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    width: 90px;
    height: 90px;
    line-height: 90px;
  }

  .el-upload-list--picture-card .el-upload-list__item-actions,
  .el-upload-list--picture-card .el-upload-list__item-thumbnail,
  .el-upload-list__item.is-success {
    width: 90px;
    height: 90px;
    line-height: 90px;
  }

  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 90px;
    height: 90px;
    line-height: 90px;
    text-align: center;
  }

  .avatar {
    width: 90px;
    height: 90px;
    display: block;
  }
</style>
